/**   
 * @Title: NewLoginCommunicater.java 
 * @Package com.esun.rabbit2.communicaters.user 
 * @Description: 新登录接口,加入登陆次数限制
 * @author: City Jiangsy@500wan.com   
 * @date: 2012-1-4 上午11:42:35  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.communicaters.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import com.esun.config.BussinessCode;
import com.esun.config.DistributionChannels;
import com.esun.config.Key;
import com.esun.model.UserInfo;
import com.esun.network.ServerCommunicater;
import com.esun.util.HashMapUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: NewLoginCommunicater
 * @Description: 新登录接口,加入登陆次数限制
 * @author: City Jiangsy@500wan.com
 * @date: 2012-1-4 上午11:42:35
 * 
 */
public class NewLoginCommunicater extends ServerCommunicater {
	private int errCount = 0;
	private int resultCode = -1;
	private boolean isLogin = false;
	private String username;
	private String userpwd;

	public boolean isLoginSucceed(String userName, String userPass, String versionName, String userimei, String useragent,String IMEI) throws ClientProtocolException, IOException, ParserConfigurationException,
			SAXException {
		username = userName;
		userpwd = userPass;
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		list.add(HashMapUtil.getMapItem(BussinessCode.PASER_MID, BussinessCode.MID_LOGIN_NEW));
		list.add(HashMapUtil.getMapItem(Key.KEY_USERNAME, userName));
		list.add(HashMapUtil.getMapItem(Key.KEY_USERPASS, userPass));
		list.add(HashMapUtil.getMapItem(Key.KEY_CHANNEL, DistributionChannels.QUDAO));
		list.add(HashMapUtil.getMapItem(Key.KEY_VERSION, versionName));
		list.add(HashMapUtil.getMapItem(Key.KEY_USERIMEI, userimei));
		list.add(HashMapUtil.getMapItem(Key.KEY_USERAGENT, useragent));
		list.add(HashMapUtil.getMapItem(Key.KEY_MBIMEI, IMEI));

		super.getServerResponse(list, true);
		if (super.getState() == 100) {
			isLogin = true;
		} else {
			isLogin = false;
			setResultCodeAndErrCount();
		}

		return isLogin;
	}

	private void setResultCodeAndErrCount() {
		try {
			resultCode = Integer.parseInt(super.getResult().get(Key.KEY_RESULTCODE));
		} catch (Exception e) {
			EDebug.o(NewLoginCommunicater.this, e.toString());
		}

		try {
			errCount = Integer.parseInt(super.getResult().get(Key.KEY_ERRCOUNT));
		} catch (Exception e) {
			EDebug.o(NewLoginCommunicater.this, e.toString());
		}
	}

	public void updataUserInfo() {
		if (isLogin) {
			UserInfo.getUserInfo().setUserName(username);
			UserInfo.getUserInfo().setUserPwd(userpwd);
			UserInfo.getUserInfo().setUserSession(super.getResult().get(BussinessCode.PASER_CK));
			UserInfo.getUserInfo().setUserBalance(super.getResult().get(Key.KEY_MONEY));
			UserInfo.getUserInfo().setUserFreeze(super.getResult().get(Key.KEY_FREEZEMONEY));
		}
	}

	/**
	 * @return the errCount
	 */
	public int getErrCount() {
		return errCount;
	}

	/**
	 * @return the resultCode
	 */
	public int getResultCode() {
		return resultCode;
	}

}
